package cn.hg.solon.youcan.job.provider;

import static cn.hg.solon.youcan.job.entity.table.SysJobLogTableDef.SYS_JOB_LOG;

import java.util.Date;
import java.util.Map;

import org.dromara.hutool.core.text.CharSequenceUtil;
import org.dromara.hutool.core.text.StrValidator;
import org.dromara.hutool.core.util.ObjUtil;
import org.noear.solon.annotation.Component;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryOrderBy;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;

import cn.hg.solon.youcan.job.entity.SysJobLog;
import cn.hg.solon.youcan.job.mapper.SysJobLogMapper;
import cn.hg.solon.youcan.job.service.SysJobLogService;


@Component
public class SysJobLogProvider extends ServiceImpl<SysJobLogMapper, SysJobLog> implements SysJobLogService {

    /* (non-Javadoc)
     * @see cn.hg.solon.youcan.job.provider.SysJobLogService#deleteBy(java.util.Map)
     */
    @Override
    public boolean deleteBy(Map<String, Object> paraMap) {
        // int count = Db.template("monitor.jobLog.deleteBy", paraMap).update();
        //
        // return count > 0;
        return false;
    }

    /* (non-Javadoc)
     * @see cn.hg.solon.youcan.job.provider.SysJobLogService#paginateBy(int, int, java.util.Map)
     */
    @Override
    public Page<SysJobLog> pageByMap(Page<SysJobLog> page, Map<String, Object> paraMap) {
        String word = (String)paraMap.get("word");
        String group = (String)paraMap.get("group");
        Date startDate = (Date)paraMap.get("startDate");
        Date endDate = (Date)paraMap.get("endDate");
        String status = (String)paraMap.get("status");
        String sortField = (String)paraMap.get("sortField");
        String sortType = (String)paraMap.get("sortType");
        QueryWrapper query = QueryWrapper.create()
            .where(SYS_JOB_LOG.GROUP.eq(group).when(StrValidator.isNotBlank(group))
                .and(SYS_JOB_LOG.STARTED_DATETIME.ge(startDate).when(ObjUtil.isNotNull(startDate)))
                .and(SYS_JOB_LOG.ENDED_DATETIME.le(endDate).when(ObjUtil.isNotNull(endDate)))
                .and(SYS_JOB_LOG.STATUS.eq(status).when(StrValidator.isNotBlank(status)))
                .and(SYS_JOB_LOG.NAME.like(word).when(StrValidator.isNotBlank(word))
                    .or(SYS_JOB_LOG.TARGET.like(word).when(StrValidator.isNotBlank(word)))
                    .or(SYS_JOB_LOG.MESSAGE.like(word).when(StrValidator.isNotBlank(word)))
                )
            );

        if (StrValidator.isNotBlank(sortField) && StrValidator.isNotBlank(sortType)) {
            query.orderBy(new QueryOrderBy(new QueryColumn(CharSequenceUtil.toUnderlineCase(sortField)), sortType));
        }

        return this.page(page, query);
    }

    @Override
    public boolean removeAll() {
        return this.remove(SYS_JOB_LOG.ID.ne(0));
    }

}
